package baperjakat;

import java.util.List;
import model.Kriteria;

/**
 *
 * @author Marlon
 */
public final class ProsesAhp {
    private List<Kriteria> listKriteria;
    private int arraySize;
    
    public ProsesAhp(List<Kriteria> list) {
        setKriteria(list);
    }
    
    public void setKriteria(List<Kriteria> list) {
        listKriteria = list;
        arraySize = list.size();
    }

    public float[][] matriksPerbandinganBerpasangan() {
        float matriks[][] = new float[arraySize][arraySize];

        for(int x = 0; x < arraySize; x++) {
            Kriteria kx = listKriteria.get(x);

            for(int y = 0; y < arraySize; y++) {
                Kriteria ky = listKriteria.get(y);
                
                float i = Float.parseFloat(kx.getBobot());
                float j = Float.parseFloat(ky.getBobot());

                matriks[x][y] = i / j;
            }
        }
        
        return matriks;
    }
    
    public float[] jumlahMatriksPerbandinganBerpasangan(float[][] matriksPerbandinganBerpasangan) {
        float[] jumlah = new float[arraySize];
        
        for(int x = 0; x < arraySize; x++) {
            for(int y = 0; y < arraySize; y++) {
                jumlah[x] += matriksPerbandinganBerpasangan[x][y];
            }
        }
        
        return jumlah;
    }

    public float[][] matriksNilaiKriteria(float[][] matriksPerbandinganBerpasangan) {
        float matriks[][] = new float[arraySize][arraySize];
        float jumlahMatriksPerbandinganBerpasangan[] = jumlahMatriksPerbandinganBerpasangan(matriksPerbandinganBerpasangan);

        for(int x = 0; x < arraySize; x++) {
            for(int y = 0; y < arraySize; y++) {
                matriks[x][y] = matriksPerbandinganBerpasangan[x][y] / jumlahMatriksPerbandinganBerpasangan[x];
            }
        }
        
        return matriks;
    }
    
    public float[] barisPrioritas(float[][] matriksNilaiKriteria) {
        float jumlah[] = new float[arraySize];
        float barisPrioritas[] = new float[arraySize];

        for(int x = 0; x < arraySize; x++) {
            for(int y = 0; y < arraySize; y++) {
                jumlah[y] += matriksNilaiKriteria[x][y];
            }
        }

        for(int x = 0; x < arraySize; x++) {
            barisPrioritas[x] = jumlah[x] / arraySize;
        }

        return barisPrioritas;
    }
    
    public float[][] matriksPenjumlahanSetiapBaris(float[] barisPrioritas, float[][] matriksPerbandinganBerpasangan) {
        float matriks[][] = new float[arraySize][arraySize];
        
        for(int x = 0; x < arraySize; x++) {
            for(int y = 0; y < arraySize; y++) {
                matriks[x][y] = matriksPerbandinganBerpasangan[x][y] * barisPrioritas[y];
            }
        }
        
        return matriks;
    }
    
    public float[] jumlahMatriksPenjumlahanSetiapBaris(float[][] matriksPenjumlahanSetiapBaris) {
        float jumlah[] = new float[arraySize];
        
        for(int x = 0; x < arraySize; x++) {
            for(int y = 0; y < arraySize; y++) {
                jumlah[y] += matriksPenjumlahanSetiapBaris[x][y];
            }
        }
        
        return jumlah;
    }

    public double rasioKonsistensi(float[] jumlahMatriksPenjumlahanSetiapBaris, float[] barisPrioritas) {
        float jumlah = 0f;
        
        for(int x = 0; x < arraySize; x++) {
            jumlah += jumlahMatriksPenjumlahanSetiapBaris[x] + barisPrioritas[x];
        }
        
        float lamdaMaksimum = jumlah / arraySize;
        float ConsistencyIndeks = (lamdaMaksimum - arraySize) / arraySize;
        double consistencyRatio = ConsistencyIndeks / getIndexRatio();
        
        return consistencyRatio;
    }
    
    public double getIndexRatio() throws NumberFormatException {
        switch(arraySize) {
            case 1: return 0;
            case 2: return 0;
            case 3: return 0.58;
            case 4: return 0.9;
            case 5: return 1.12;
            case 6: return 1.24;
            case 7: return 1.32;
            case 8: return 1.41;
            case 9: return 1.45;
            case 10: return 1.49;
            case 11: return 1.51;
            case 12: return 1.48;
            case 13: return 1.56;
            case 14: return 1.57;
            case 15: return 1.59;
            default: throw new NumberFormatException("Ukuran matriks maksimal adalah 15");
        }
    }
    
    public void cetak(float matriks[][], float[] jumlah) {
        int x = matriks.length;
        
        for(int i = 0; i < x; i++) {
            for(int j = 0; j < x; j++) {
                System.out.print(matriks[i][j]);
                System.out.print(" | ");
            }
            System.out.println();
        }
        
        for(int i = 0; i < x; i++) {
            System.out.print(jumlah[i]);
            System.out.print(" | ");
        }
    }

    public void cetak(float matriks[][]) {
        int x = matriks.length;
        
        for(int i = 0; i < x; i++) {
            for(int j = 0; j < x; j++) {
                System.out.print(matriks[i][j]);
                System.out.print(" | ");
            }
            System.out.println();
        }
    }
}
